package com.acme.bankapp.domain.bank.account;

import com.acme.bankapp.service.bank.exception.NotEnoughFundsException;

/**
 *
 * @author nulls.narik
 *
 */
@SuppressWarnings("serial")
abstract class AbstractAccount implements Account, java.io.Serializable {
	private final static String MESSAGE_ERROR_BALANCE = "Balance less zero";

	protected final static String MESSAGE_ERROR_ARGUMENT = "Error argument, less zero";

	protected double balance;

	public AbstractAccount(double balance) throws IllegalArgumentException {
		if (balance < 0) {
			throw new IllegalArgumentException(MESSAGE_ERROR_BALANCE);
		}
		this.balance = balance;
	}

	public double getBalance() {
		return balance;
	}

	public void deposit(double x) throws IllegalArgumentException {
		if (x < 0) {
			throw new IllegalArgumentException(MESSAGE_ERROR_ARGUMENT);
		}
		balance += x;
	}

	public void withdraw(double x) throws IllegalArgumentException, NotEnoughFundsException {
		if (x < 0) {
			throw new IllegalArgumentException(MESSAGE_ERROR_ARGUMENT);
		}
		if (balance < x) {
			throw new NotEnoughFundsException();
		}

		balance -= x;
	}

	public double maximumAmountToWithdraw() {
		return balance;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		long temp;
		temp = Double.doubleToLongBits(balance);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		AbstractAccount other = (AbstractAccount) obj;
		if (balance != other.balance)
			return false;
		return true;
	}

	@Override
	public String toString() {
		return String.valueOf(balance);
	}

	public double decimalValue() {
		return Math.floor(balance);
	}
}
